#include "view_system.h"
#include "prj_typedef.h"
#include "blecom.h"
#include "display_ctrl.h"
#include "view_abstract.h"
#include "platform_assert.h"
#include "times/view_times.h"
#include "heart_rate/view_heart_rate.h"
#include "others/view_others.h"
#include "metronome/view_metronome.h"
#include "sports/view_sports.h"
#include "relax/view_relax.h"
#include "app_trace.h"
#include "view_prompt.h"
#include "view_system.h"
#include "prj_typedef.h"
#include "motor.h"
#include "prj_config.h"
#include "display_interface_240x240.h"
#include "lfs_file_manager_auto.h"
#include "heart_rate/view_heart_rate.h"
#include "stopwatch.h"
#include "heart_rate_interface.h"
#include "rtc.h"
#include "view_common.h"
#include "font/num_font.h"
#include "app_trace.h"
#include "m_product_test.h"
#include "font_common.h"
#ifdef UI_BLOOD_PRESSURE
#include "blood_pressure/view_blood_pressure.h"
#endif
#ifdef UI_BLOOD_OXYGEN
#include "blood_oxygen/view_blood_oxygen.h"
#endif
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_power_on_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (product_test_state_get() != PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            display_show(view_untested, DISP_MODE_NORMAL);
            return;
        }
        disp_ctrl.auto_refresh_time = 500;
        if (disp_ctrl.auto_refresh_count >= 5)
        {
            display_show(view_time, DISP_MODE_DOWN_MOVE);
            return;
        }
    }
    if (disp_ctrl.auto_refresh_count > 4)
    {
#if 0//defined(SM02_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 51, 39, FILE_IMG_1201_hello_5_51_49);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 5, 99, FILE_IMG_J319_boot_1_5_99);
#endif
    }
    else
    {
#if 0//defined(SM02_BOARD)
        int const x = 51;
        int const y = 39;
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 5,
            .img[0].x = x,
            .img[0].y = y,
            .img[0].sn = FILE_IMG_1201_hello_1_51_49,
            .img[1].x = x,
            .img[1].y = y,
            .img[1].sn = FILE_IMG_1201_hello_2_51_49,
            .img[2].x = x,
            .img[2].y = y,
            .img[2].sn = FILE_IMG_1201_hello_3_51_49,
            .img[3].x = x,
            .img[3].y = y,
            .img[3].sn = FILE_IMG_1201_hello_4_51_49,
            .img[4].x = x,
            .img[4].y = y,
            .img[4].sn = FILE_IMG_1201_hello_5_51_49,
        };
#else
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 5,
            .img[0].x = 0,
            .img[0].y = 99,
            .img[0].sn = FILE_IMG_J315_boot_1_0_99,
            .img[1].x = 0,
            .img[1].y = 99,
            .img[1].sn = FILE_IMG_J316_boot_2_0_99,
            .img[2].x = 3,
            .img[2].y = 99,
            .img[2].sn = FILE_IMG_J317_boot_1_3_99,
            .img[3].x = 4,
            .img[3].y = 99,
            .img[3].sn = FILE_IMG_J318_boot_1_4_99,
            .img[4].x = 5,
            .img[4].y = 99,
            .img[4].sn = FILE_IMG_J319_boot_1_5_99,
        };
#endif
        _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_0);
    }
}
void view_power_off_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.auto_refresh_count >= 5)
    {
        return;
    }
    else
    {
#if 0//defined(SM02_BOARD)
        int const x = 25;
        int const y = 42;
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 5,
            .img[0].x = x,
            .img[0].y = y,
            .img[0].sn = FILE_IMG_1101_seeyou_1_25_52,
            .img[1].x = x,
            .img[1].y = y,
            .img[1].sn = FILE_IMG_1101_seeyou_2_25_52,
            .img[2].x = x,
            .img[2].y = y,
            .img[2].sn = FILE_IMG_1101_seeyou_3_25_52,
            .img[3].x = x,
            .img[3].y = y,
            .img[3].sn = FILE_IMG_1101_seeyou_4_25_52,
            .img[4].x = x,
            .img[4].y = y,
            .img[4].sn = FILE_IMG_1101_seeyou_5_25_52,
        };
#else
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 5,
            .img[0].x = 0,
            .img[0].y = 99,
            .img[0].sn = FILE_IMG_J310_poweroff_1_0_99,
            .img[1].x = 0,
            .img[1].y = 99,
            .img[1].sn = FILE_IMG_J311_poweroff_2_0_99,
            .img[2].x = 0,
            .img[2].y = 99,
            .img[2].sn = FILE_IMG_J312_poweroff_3_0_99,
            .img[3].x = 0,
            .img[3].y = 99,
            .img[3].sn = FILE_IMG_J313_poweroff_4_0_99,
            .img[4].x = 0,
            .img[4].y = 99,
            .img[4].sn = FILE_IMG_J314_poweroff_5_0_99,
        };
#endif
        _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_0);
    }
}
bool view_power_on_event(event_param_t param)
{
    if (param.event == BUTTON_EVENT_LONG_PRESS)
    {
        power_on();
        motor_start(500);
        display_show(view_power_on_animation, DISP_MODE_NORMAL);
        return true;
    }
    return false;
}

void view_untested_display(void)
{
    if (disp_ctrl.auto_refresh_count == 0)
    {
        disp_ctrl.auto_close_delay = 10000;
    }
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
    }

    int x = 83;
    int y = 10;
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }

    int const device_y_interval = 55;
    int const name_start_y = 40;
    int const hw_start_y = name_start_y; //+ device_y_interval;
    int const fw_start_y = name_start_y + device_y_interval * 1;
    int const mac_start_y = name_start_y + device_y_interval * 2;

    uint8_t hardware[32] = {0};
    uint8_t firmware[32] = {0};
    strcpy((char *)hardware, DEVICE_PREFIX);
    sprintf((char *)hardware, "%s_V%s", hardware, HARDWARE_VERSION);
    sprintf((char *)firmware, "%s_%s", FIRMWARE_VERSION, FW_STRING_DATE);

    //硬件版本号
    disp_show_user_string_middle(0, 240, hw_start_y, (char *)hardware, num_hex_12x28_get_bmp);

    //固件版本号
    disp_show_user_string_middle(0, 240, fw_start_y, (char *)firmware, num_hex_12x28_get_bmp);

    //蓝牙地址
    char buffer[32] = {0};
    ble_gap_addr_t p_addr;
    sd_ble_gap_address_get(&p_addr);
    sprintf(buffer, "%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X", p_addr.addr[5] >> 4, p_addr.addr[5] & 0xf, p_addr.addr[4] >> 4, p_addr.addr[4] & 0xf, p_addr.addr[3] >> 4, p_addr.addr[3] & 0xf,
            p_addr.addr[2] >> 4, p_addr.addr[2] & 0xf, p_addr.addr[1] >> 4, p_addr.addr[1] & 0xf, p_addr.addr[0] >> 4, p_addr.addr[0] & 0xf);

    disp_show_user_string_middle(0, 240, mac_start_y, buffer, num_hex_9x19_get_bmp); //总长度7+16+14=37*6-7=215
}
void view_screen_test_display(void)
{
    disp_draw_rect_color(0, 0, OLED_SIZE_X - 1, OLED_SIZE_Y - 1, RGB565_COLOR_WHITE);
}
bool view_screen_test_event(event_param_t param)
{
    switch (param.event)
    {
    case BUTTON_EVENT_SHORT_PRESS:
        display_show(view_time, DISP_MODE_DOWN_MOVE);
        return true;
    }
    return false;
}
void view_heart_rate_test_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[2] = APPhealth_Type;
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        disp_ctrl.com_data.i_temp[0] = 30 - disp_ctrl.auto_refresh_count / 2;
        if (disp_ctrl.com_data.i_temp[0] < 0)
        {
            disp_ctrl.com_data.i_temp[0] = 0;
        }
        if (disp_ctrl.auto_refresh_count > 35 * 2)
        {
            LOG("test timeout [%d]", time);
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        if (!disp_ctrl.com_data.i_temp[1])
        {
            //!未佩戴
            if (disp_ctrl.com_data.i_temp[4] == -1)
            {
                disp_ctrl.auto_refresh_count = disp_ctrl.auto_refresh_count;
                LOG("no wear start time [%d]", disp_ctrl.auto_refresh_count);
            }
            else
            {
                if (disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4] > 3 * 2)
                {
                    LOG("no wear timeout [%d]", disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4]);
                    display_show_type_recover();
                    return;
                }
            }
        }
        else
        {
            disp_ctrl.com_data.i_temp[4] = -1;
        }
    }
    switch (disp_ctrl.com_data.i_temp[2])
    {
    case APP_HEALTH_TEST_ONCE_HR:
    {
        view_heart_rate_param const param = {
            .is_wear = disp_ctrl.com_data.i_temp[1],
            .num = disp_ctrl.com_data.i_temp[0],
            .num_digit = 2,
            .unit_index = FILE_IMG_H105_heartrate_s,
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .unit_height = 27,
            .unit_width = 13,
            .temp_index = 50,
        };
        view_heart_rate_animation(&param);
    }
    break;
#ifdef UI_BLOOD_PRESSURE
    case APP_HEALTH_TEST_BLOOD_PRESSURE:
    {
        view_blood_pressure_param const param = {
            .is_wear = disp_ctrl.com_data.i_temp[1],
            .seconds = disp_ctrl.com_data.i_temp[0],
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_pressure_animation(&param);
    }
    break;
#endif
#ifdef UI_BLOOD_OXYGEN
    case APP_HEALTH_TEST_OXIMETER:
    {
        view_blood_oxygen_param const param = {
            .is_wear = disp_ctrl.com_data.i_temp[1],
            .seconds = disp_ctrl.com_data.i_temp[0],
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_oxygen_animation(&param);
    }
    break;
#endif
    }
}
bool view_heart_rate_test_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 40000;
        return true;
    case event_view_exit:
        APPhealth_Stop();
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return true;
    case event_ble_disconnect:
        // case event_move_level_up:
        display_show_type_recover();
        LOG("view_heart_rate_test cancel");
        return true;
    }
    return false;
}
void view_heart_rate_result_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[2] = APPhealth_Type;

        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;

        disp_ctrl.com_data.i_temp[0] = app_health_test_result[0];
        if (disp_ctrl.auto_refresh_count > 5 * 2)
        {
            LOG("test result timeout [%d]", time);
            display_show_type_recover();
            return;
        }
    }
    switch (disp_ctrl.com_data.i_temp[2])
    {
    case APP_HEALTH_TEST_ONCE_HR:
    {
        view_heart_rate_param const param = {
            .is_wear = true,
            .num = disp_ctrl.com_data.i_temp[0],
            .num_digit = 0,
            .unit_index = FILE_IMG_H104_heartrate_unit_bpm_129_159,
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .unit_height = 22,
            .unit_width = 39,
            .temp_index = 50,
        };
        view_heart_rate_animation(&param);
    }
    break;

#ifdef UI_BLOOD_PRESSURE
    case APP_HEALTH_TEST_BLOOD_PRESSURE:
    {
        view_blood_pressure_param const param = {
            .is_wear = true,
            .seconds = -1,
            .dbp = disp_ctrl.com_data.i_temp[0],
            .sbp = disp_ctrl.com_data.i_temp[1],
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_pressure_animation(&param);
    }
    break;
#endif
#ifdef UI_BLOOD_OXYGEN
    case APP_HEALTH_TEST_OXIMETER:
    {
        view_blood_oxygen_param const param = {
            .is_wear = true,
            .seconds = -1,
            .percent = disp_ctrl.com_data.i_temp[0],
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_oxygen_animation(&param);
    }
    break;
#endif
    }
}

bool view_heart_rate_test_result_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 10000;
        stopwatch_start();
        LOG("view_heart_rate_test entry");
        return true;
    case event_view_exit:
        stopwatch_stop();
        LOG("view_heart_rate_test exit");
        return true;
    }
    return false;
}
bool view_system_display(int view)
{
    switch (view)
    {
    case view_heart_rate_test:
        view_heart_rate_test_display();
        return true;
    case view_heart_rate_result:
        view_heart_rate_result_display();
        return true;
    case view_power_on_animation:
        view_power_on_display();
        return true;
    case view_power_off_animation:
        view_power_off_display();
        return true;
    case view_untested:
        view_untested_display();
        return true;
    case view_screen_test:
        view_screen_test_display();
        return true;
    }
    return false;
}
bool view_system_event(int view, event_param_t param)
{
    bool result = false;
    switch (view)
    {
    case view_screen_test:
        result = view_screen_test_event(param);
        break;
    case view_heart_rate_test:
        result = view_heart_rate_test_event(param);
        break;
    case view_heart_rate_result:
        result = view_heart_rate_test_result_event(param);
        break;
    }
    return result;
}
